﻿using System;
using System.Collections.Generic;
using System.IO;
using CSJ2K.Util.IO;

namespace CSJ2K.Jpc.Segments
{
    /// <summary>Start of packet (SOP)</summary>
    /// <comment>
    /// Function: Marks the beginning of a packet within a codestream.  
    /// Usage: Optional. May be used in the bit stream in front of every packet. Shall not 
    ///      be used unless indicated that it is allowed in the proper COD marker segment 
    ///      (see A.6.1). If PPM or PPT marker segments are used, then the SOP marker 
    ///      segment may appear immediately before the packet data in the bit stream. If 
    ///      SOP marker segments are allowed (by signalling in the COD marker segment, see
    ///      A.6.1), each packet in any given tile-part may or may not be appended with an
    ///      SOP marker segment. However, whether or not the SOP marker segment is used, 
    ///      the count in the Nsop is incremented for each packet. If the packet headers 
    ///      are moved to a PPM or PPT marker segments (see A.7.4 and A.7.5), then the SOP 
    ///      marker segments may appear immediately before the packet body in the tile-part
    ///      compressed image data portion. 
    /// Length: Fixed.  
    /// </comment>
    public class SOP : Segment
    {
        /// <summary>Packet sequence number.</summary>
        public ushort N;

        public SOP(Stream source)
            : this(source, (Markers)StreamUtil.ReadBUInt16(source)) { }
        public SOP(Stream source, Markers marker)
            : base(source, marker, true)
        {
            if (marker != Markers.SOP) throw (new ApplicationException("Attempting to create segment of the wrong type."));

            N = StreamUtil.ReadBUInt16(source);
        }
    }
}
